<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\FrictionEvaluation;

class FrictionEvaluationController extends Controller
{
    function save(Request $request){
        $data = $request->input('data') ;
        if(empty($data)){
            return json_encode(array('code' => '1', 'msg' => 'empty request data'));
        }
        //first check if the items is already exist in DB, if not, we create a new record
        $frictionEvaluation = FrictionEvaluation::where('cea_id', $data['cea_id'])
                                    ->first();
        if(!$frictionEvaluation){
            $frictionEvaluation = new FrictionEvaluation();
        }

        $frictionEvaluation->cea_id = isset($data['cea_id']) ? $data['cea_id'] : ' ';
        $frictionEvaluation->user = isset($data['openId']) ? $data['openId'] : ' ';
        $frictionEvaluation->like = isset($data['suggestionLike']) ? $data['suggestionLike'] : ' ';
        $frictionEvaluation->suggesstion = isset($data['suggestionText']) ? $data['suggestionText'] : ' ';
        $frictionEvaluation->agree = ' ';
        $frictionEvaluation->share_id = ' ';
        $result = $frictionEvaluation->save();
        if (!$result) {
            return json_encode(array('code' => '2', 'msg' => 'data save failed'));
        } else {
            return json_encode(array('code' => '0', 'msg' => 'data save success', 'result' => $frictionEvaluation));
        }

    }

    function sharePageItemSave(Request $request){
        $data = $request->input('data') ;
        if(empty($data)){
            return json_encode(array('code' => '1', 'msg' => 'empty request data'));
        }
        //first check if the items is already exist in DB, if not, we create a new record
        $frictionEvaluation = FrictionEvaluation::where('cea_id', $data['cea_id'])
                                                    ->where('user', $data['openId'])
                                                    ->where('share_id', $data['shareOpenId'])
                                                    ->first();
        if(!$frictionEvaluation){
            $frictionEvaluation = new FrictionEvaluation();
        }

        $frictionEvaluation->cea_id = isset($data['cea_id']) ? $data['cea_id'] : ' ';
        $frictionEvaluation->user = isset($data['openId']) ? $data['openId'] : ' ';
        $frictionEvaluation->like = isset($data['suggestionLike']) ? $data['suggestionLike'] : ' ';
        $frictionEvaluation->suggesstion = isset($data['suggestionText']) ? $data['suggestionText'] : ' ';
        $frictionEvaluation->agree = ' ';
        $frictionEvaluation->share_id =  $data['shareOpenId'];
        $result = $frictionEvaluation->save();
        if (!$result) {
            return json_encode(array('code' => '2', 'msg' => 'data save failed'));
        } else {
            return json_encode(array('code' => '0', 'msg' => 'data save success', 'result' => $frictionEvaluation));
        }
    }
}
